package com.zhiyou100.video.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhiyou100.video.model.Course;
import com.zhiyou100.video.service.CourseService;
import com.zhiyou100.video.service.SubjectService;

@Controller
@RequestMapping("/admin")
public class CourseController {
	@Autowired
	private CourseService cs;
	
	@Autowired
	private SubjectService ss;
	
	@RequestMapping("/course")
	public String course(String course,String speaker,Model model) {
		Map<String, Object> map = new HashMap<>();
		map.put("course", course);
		map.put("speaker", speaker);
		List<Course> list = cs.selectCourse();
		model.addAttribute("courses", list);
		
		return "backstage/course/course";
	}
	
	
	@RequestMapping("/addcourse")
	public String add(Model model) {
		model.addAttribute("subjects", ss.selectAll());
		return "backstage/course/edit_course";
	}

	@RequestMapping("/course/insert")
	public String insert(Course course) {
		Date date = new Date();
		course.setInsert_time(date);
		int i = cs.insertCourse(course);
		System.out.println("++++++++++++++++"+i);
		if(i>0) {
			return "redirect:../course";
		}else {
			return "backstage/course/edit_course";
		}
	}
	
	@RequestMapping("/upcourse")
	public String updatespeaker(int id,Model model) {
		Course course = cs.selectById(id);
		model.addAttribute("course", course);
		model.addAttribute("subjects", ss.selectAll());
		return "backstage/course/edit_course";
	}
	
	
	@RequestMapping("/course/update")
	public String update(Course course,Model model) {
		Date date = new Date();
		course.setUpdate_time(date);
		int i = cs.updateCourse(course);
		System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"+i);
		if(i>0) {
			return "redirect:../course";
		}
		return "backstage/course/edit_course";
	}
	
	@RequestMapping("/course/delete")
	public String deleteById(int id) {
		int i = cs.deleteById(id);
		if(i>0) {
			return "redirect:../course";
		}
		return null;
	}
	
	
}
